﻿using System;
using System.Web;
using OpenPhotoAlbum.Controls;

namespace OpenPhotoAlbum.Shared
{
	public partial class Shared : System.Web.UI.MasterPage
	{
		private DbAccess.User _user;

		protected override void OnInit(EventArgs e)
		{
			base.OnInit(e);

			ltrTitle.Text = StringUtility.FormatAndHtmlEncode(
				Resources.Strings.Shared_Title,
				this.User.UserName);
			ltrAddress.Text = HttpUtility.HtmlEncode(
				Links.ToFullPath(UrlContext.BuildSharedHomeUrl(this.User.UserName)));
			hlEditMyAlbum.Text = Resources.Strings.Shared_EditMyAlbum;
			hlEditMyAlbum.NavigateUrl = UrlContext.BuildMyHomeUrl(0);
		}

        public DbAccess.User User
        {
            get
            {
                if (_user == null)
                {
					if (UrlContext.UserId == null || UrlContext.UserId.Equals(Guid.Empty))
					{
						if (UrlContext.AlbumId == null || UrlContext.AlbumId.Equals(Guid.Empty))
						{
							if (UrlContext.PhotoId == null || UrlContext.PhotoId.Equals(Guid.Empty))
							{

							}
							else
							{
								_user = BusinessLogics.OwnerCache.Instance.GetPhotoOwner(UrlContext.PhotoId);
							}
						}
						else
						{
							_user = BusinessLogics.OwnerCache.Instance.GetAlbumOwner(UrlContext.AlbumId);
						}
					}
					else
					{
						_user = BusinessLogics.OwnerCache.Instance.GetUser(UrlContext.UserId);
					}
                }
				return _user;
            }
        }

		public string BuddyIconImageUrl
		{
			get
			{
				Guid userId = Guid.Empty;

				if (!UrlContext.UserId.Equals(Guid.Empty))
				{
					userId = UrlContext.UserId;
				}
				else if (!UrlContext.AlbumId.Equals(Guid.Empty))
				{
					userId = BusinessLogics.AlbumManager.Instance.GetUserId(UrlContext.AlbumId);
				}
				else if (!UrlContext.PhotoId.Equals(Guid.Empty))
				{
					userId = BusinessLogics.PhotoManager.Instance.GetUserId(UrlContext.PhotoId);
				}

				return UrlContext.BuildPhotoPipeUserIconUrl(userId); 
			}
		}
	}

	public class UserNotFoundException : Exception { }
}
